package com.qisimanxiang.dalaran.zuul.custom.ribbon;

import com.netflix.zuul.context.RequestContext;
import com.qisimanxiang.dalaran.zuul.config.SentinelConfig;
import com.qisimanxiang.dalaran.zuul.param.PublicParam;
import com.qisimanxiang.dalaran.zuul.util.ContextConstant;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient;
import org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;

import java.util.Collections;
import java.util.Set;

import static com.qisimanxiang.dalaran.zuul.util.ContextConstant.CUSTOMER_SERVICE_RT_KEY;
import static com.qisimanxiang.dalaran.zuul.util.ContextConstant.PUBLIC_PARAM_KEY;


/**
 * @author wangmeng
 * @date created in 3:48 下午 2019/9/17
 * @modified by
 */
public class CustomHttpClientRibbonCommandFactory extends AbstractRibbonCommandFactory {

    private final SpringClientFactory clientFactory;

    private final ZuulProperties zuulProperties;

    public CustomHttpClientRibbonCommandFactory(SpringClientFactory clientFactory,
                                                ZuulProperties zuulProperties) {
        this(clientFactory, zuulProperties, Collections.<FallbackProvider>emptySet());
    }

    public CustomHttpClientRibbonCommandFactory(SpringClientFactory clientFactory,
                                                ZuulProperties zuulProperties, Set<FallbackProvider> fallbackProviders) {
        super(fallbackProviders);
        this.clientFactory = clientFactory;
        this.zuulProperties = zuulProperties;
    }

    @Override
    public CustomHttpClientRibbonCommand create(final RibbonCommandContext context) {
        FallbackProvider zuulFallbackProvider = getFallbackProvider(
                context.getServiceId());
        final String serviceId = context.getServiceId();
        final RibbonLoadBalancingHttpClient client = this.clientFactory
                .getClient(serviceId, RibbonLoadBalancingHttpClient.class);
        client.setLoadBalancer(this.clientFactory.getLoadBalancer(serviceId));
        RequestContext currentContext = RequestContext.getCurrentContext();
        String contextUri = (String) currentContext.get(FilterConstants.REQUEST_URI_KEY);
        PublicParam publicParam = (PublicParam) currentContext.get(PUBLIC_PARAM_KEY);

        //获取用户接口RT
        Integer customerServiceRt = SentinelConfig.getCustomerServiceRt(contextUri, publicParam.getCustomerId());
        //获取用户接口RT
        int rt = customerServiceRt == null ? ContextConstant.CUSTOMER_SERVICE_RT_DEFAULT : customerServiceRt;
        String commandKey = contextUri + "::" + publicParam.getCustomerId() + "::" + rt;
        return new CustomHttpClientRibbonCommand(commandKey, client, context, zuulProperties,
                zuulFallbackProvider, clientFactory.getClientConfig(serviceId), rt);
    }

}
